/**
 * 
 */
package com.java.test.util;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 *
 */
public class DateUtil {

	public static String f1 = "yyyy-MM-dd HH:mm:ss";

	// xxxx年xx月xx日
	public static String f2 = "yyyyMMdd";

	// xxxx年xx-月xx-日
	public static String f3 = "yyyy-MM-dd";

	// 年月日小时
	public static String f4 = "yyyy-MM-dd HH";

	public static String convertTimeToString(Long time) {
		DateTimeFormatter ftf = DateTimeFormatter.ofPattern(DateUtil.f1);
		return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
	}

	public static List<String> getTwoDaysDay(String dateStart, String dateEnd) {

		if ((dateStart == null || "".equals(dateStart)) && (dateEnd == null || "".equals(dateEnd))) {
			return null;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		List<String> dateList = new ArrayList<String>();

		try {
			Date dateOne = sdf.parse(dateStart);
			Date dateTwo = sdf.parse(dateEnd);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dateOne);

			dateList.add(sdf.format(dateOne));
			while (calendar.getTime().before(dateTwo)) {
				calendar.add(Calendar.DAY_OF_MONTH, +1);
				dateList.add(sdf.format(calendar.getTime()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateList;
	}

	/**
	 * 比较两个时间是否在同一个小时
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean dateAmorethandateB(LocalDateTime a, LocalDateTime b) {

		if (a == null || b == null) {
			return false;
		}

		int aY = a.getYear();
		int aM = a.getMonth().getValue();
		int aD = a.getDayOfMonth();
		int aH = a.getHour();
		int aMin = a.getMinute();

		int bY = b.getYear();
		int bM = b.getMonth().getValue();
		int bD = b.getDayOfMonth();
		int bH = b.getHour();
		int bMin = b.getMinute();

		int adate[] = new int[] { aY, aM, aD, aH, aMin };
		int bdate[] = new int[] { bY, bM, bD, bH, bMin };
		

		for (int i = 0; i < adate.length; i++) {

			if (adate[i] != bdate[i]) {
				return false;
			}
		}

		return true;
	}

	/**
	 * 获得当天零时零分零秒
	 * @auth hank
	 * @return 当天零时零分零秒的Date
	 */
	public static Date getToDayInitial() {
		//带着时区(默认时区,本地日期)
		ZonedDateTime zonedDateTime = ZonedDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
		//1.zonedDateTime.toInstant()转换成时间戳(因为zonedDateTime包含了时区和时间信息,可以转换为GMT的时间戳(时间+时区))2.Date.from会使用默认时区转换时间戳为本地时间(默认时区)
		Date date = Date.from(zonedDateTime.toInstant());
		return date;
	}
	
}
